package com.jh.fcsm.controller.sys;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.PageInfo;

import com.jh.fcsm.beans.sys.SysDatasource;
import com.jh.fcsm.beans.sys.vo.SysDatasourceVo;
import com.jh.fcsm.common.RestApiResponse;
import com.jh.fcsm.common.annotation.RepeatSubAnnotation;
import com.jh.fcsm.common.annotation.SystemLogAnnotation;
import com.jh.fcsm.common.BaseController;
import com.jh.fcsm.service.sys.SysDatasourceService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 数据源Controller
 * 
 * @author linq
 * @date 2023-08-11
 */
@RestController
@RequestMapping("/sys/datasource")
@Api(value="数据源", description="数据源")
public class SysDatasourceController extends BaseController{
    @Autowired
    private SysDatasourceService sysDatasourceService;

	private static final String PER_PREFIX = "btn:sys:datasource:";
	
	/**
	 * 新增数据源
	 *@param sysDatasource 数据源数据 json
	 *@return RestApiResponse<?>
	 *@author linq
	 */
	@RepeatSubAnnotation
	@PostMapping("/save")
	@ApiOperation(value = "新增数据源", notes = "新增数据源")
	@SystemLogAnnotation(type = "数据源",value = "新增数据源")
	@PreAuthorize("hasAuthority('"+PER_PREFIX+"save')")
	public RestApiResponse<?> saveSysDatasource(@RequestBody SysDatasource sysDatasource) {
		String id = sysDatasourceService.saveOrUpdateSysDatasource(sysDatasource);
		return RestApiResponse.ok(id);
	}
	
	/**
	 * 修改数据源
	 *@param sysDatasource 数据源数据 json
	 *@return RestApiResponse<?>
	 *@author linq
	 */
	@RepeatSubAnnotation
	@PostMapping("/update")
	@ApiOperation(value = "修改数据源", notes = "修改数据源")
	@SystemLogAnnotation(type = "数据源",value = "修改数据源")
	@PreAuthorize("hasAuthority('"+PER_PREFIX+"update')")
	public RestApiResponse<?> updateSysDatasource(@RequestBody SysDatasource sysDatasource) {
		String id = sysDatasourceService.saveOrUpdateSysDatasource(sysDatasource);
		return RestApiResponse.ok(id);
	}
	
	/**
	 * 批量删除数据源(判断 关联数据是否可以删除)
	 *@param ids
	 *@return RestApiResponse<?>
	 *@author linq
	 */
	@RepeatSubAnnotation
	@PostMapping("/delete")
	@ApiOperation(value = "批量删除数据源", notes = "批量删除数据源")
	@SystemLogAnnotation(type = "数据源",value = "批量删除数据源")
	@PreAuthorize("hasAuthority('"+PER_PREFIX+"delete')")
	public RestApiResponse<?> deleteSysDatasource(@RequestBody List<String> ids) {
		//判断 关联数据是否可以删除
		sysDatasourceService.deleteSysDatasource(ids);
		return RestApiResponse.ok();
	}
	
	/**
	 * 查询数据源详情
	 *@param id
	 *@return RestApiResponse<?>
	 *@author linq
	 */
	@GetMapping("/findById")
	@ApiOperation(value = "查询数据源详情", notes = "查询数据源详情")
	@PreAuthorize("hasAuthority('"+PER_PREFIX+"find')")
	public RestApiResponse<?> findById(@RequestParam("id") String id) {
		SysDatasource  sysDatasource=sysDatasourceService.findById(id);
		return RestApiResponse.ok(sysDatasource);
	}
	
	/**
	 * 分页查询数据源
	 *@param sysDatasourceVo 数据源 查询条件
	 *@return RestApiResponse<?>
	 *@author linq
	 */
	@PostMapping("/findPageByQuery")
	@ApiOperation(value = "分页查询数据源", notes = "分页查询数据源")
	@PreAuthorize("hasAuthority('"+PER_PREFIX+"query')")
	public RestApiResponse<?> findPageByQuery(@RequestBody SysDatasourceVo sysDatasourceVo) {
		PageInfo<SysDatasource>  sysDatasource=sysDatasourceService.findPageByQuery(sysDatasourceVo);
		return RestApiResponse.ok(sysDatasource);
	}
	
}
